home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Interactive 7
/
PC World Interactive 7.iso
/
program
/
pasprog.EXE
/
POSTFIX.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1980-01-10
|
3KB
|
94 lines
Program postfix;
uses crt;
const
maxstack = 100;
type
basetype = char;
stack = record
item : array[1..maxstack] of basetype;
top : 0..maxstack;
end;
var
i:Byte;
s:stack;
str:string;
operat_0,operat_1:Char;
underflow,overflow:boolean;
temporary_number:Byte;
function empty(s:stack):boolean;
begin
if s.top=0 then empty:=true
else empty:=false;
end;
procedure pop(var s:stack;var x:basetype;var underflow:boolean);
begin
if empty(s) then underflow:=true
else begin
x:=s.item[s.top];
s.top :=s.top-1;
underflow:=false;
end;
end;
procedure push(var s:stack;x:basetype;var overflow:boolean);
begin
If s.top>=maxstack then overflow:=true
else begin
overflow:=false;
s.top:=s.top+1;
s.item[s.top]:=x;
end;
end;
Procedure error_occured;
begin
writeln ('Geçersiz giriƒ yapìldì.');
writeln (' Sistem halt etti! ');
HALT;
end;
begin
textbackground(15);textcolor(0);
repeat
ClrScr;
temporary_number:=1;
s.top:=0;
write ('Postfix ifadeyi giriniz...:');
readln(str);
If length(str)<1 then error_occured;
For i:=1 to length(str) do
if not (str[i] in ['a'..'z','A'..'Z','-','+','*','/'])then error_occured;
for i:=1 to length(str) do
begin
if str[i] in ['+','-','*','/'] then
begin
pop (s,operat_0,underflow);
if underflow then error_occured;
pop (s,operat_1,underflow);
if underflow then error_occured;
write ('LD ');
if operat_1<'A' then writeln ('TEMP_',ord(operat_1))
else writeln (operat_1);
case str[i] of
'+' : write ('ADD ');
'-' : write ('SUB ');
'*' : write ('MUL ');
'/' : write ('DIV ');
end;
if operat_0<'A' then writeln ('TEMP_',ord(operat_0))
else writeln (operat_0);
push (s,chr(temporary_number),overflow);
if (overflow) then error_occured;
writeln ('ST TEMP_',ord(temporary_number));
temporary_number:=temporary_number+1;
end
else begin
push (s,str[i],overflow);
if (overflow) then error_occured;
end;
end;
writeln('ÿfade deºerlendirildi, kod üretildi...');
writeln('Çìkmak için X'e , devam için herhangi bir tuƒa basìnìz.');
until (readKey='X') or (readKey='x');
end.